import 'chat_message.dart';

class ChatSession {
  final String id;
  final List<ChatMessage> _messages;
  final DateTime createdAt;
  String title;

  ChatSession({
    required this.id,
    List<ChatMessage>? messages,
    required this.createdAt,
    this.title = '',
  }) : _messages = messages ?? [];

  List<ChatMessage> get messages => _messages;

  void addMessage(ChatMessage message) {
    _messages.add(message);
  }

  void updateLastMessage(ChatMessage message) {
    if (_messages.isNotEmpty) {
      _messages[_messages.length - 1] = message;
    }
  }

  void removeLastMessage() {
    if (_messages.isNotEmpty) {
      _messages.removeLast();
    }
  }

  String get displayTitle {
    if (title.isNotEmpty) return title;
    if (_messages.isNotEmpty) {
      return _messages.first.content.length > 20 
          ? '${_messages.first.content.substring(0, 20)}...'
          : _messages.first.content;
    }
    return '新对话';
  }
} 